/**
 * @class UI管理器
 * @author DuskyHuang 圣子
*/

import { singleton } from '../utils/decorators';
import BaseUI from '../ui/base';
import { NetCover } from "../ui/net-cover";
import { LoadingPage } from "../ui/loading-page";
import { inner } from "../core/boot";
import { __cfw_notifications__ } from "../configs/notifications";

@singleton
export default class UIManager {

    declare public static readonly instance: UIManager;

    private _netCover: NetCover = null;
    public get netCover(): NetCover | null { return this._netCover; }
    private _loadingPage: LoadingPage = null;
    public get loadingPage(): LoadingPage | null { return this._loadingPage; }

    public register<T extends BaseUI>(arg: T, autoHide: boolean = true): T {
        if (autoHide) arg.hide();
        if (arg instanceof NetCover) return this._netCover = arg;
        else if (arg instanceof LoadingPage) return this._loadingPage = arg;
    }

    public unregister(arg: BaseUI | Class<BaseUI>): bool.exist {
        if (arg instanceof NetCover && this._netCover === arg) return !(this._netCover = null);
        if (arg instanceof LoadingPage && this._loadingPage === arg) return !(this._loadingPage = null);
        if (arg === NetCover) return !(this._netCover = null);
        if (arg === LoadingPage) return !(this._loadingPage = null);
        return false;
    }
}

const ui = UIManager.instance;
inner.attach(__cfw_notifications__.UI.REGISTER, ui.register, ui);
inner.attach(__cfw_notifications__.UI.UNREGISTER, ui.unregister, ui);